package com.yuanfeng.activity.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yuanfeng.activity.dto.ActivityExplainParamDTO;
import com.yuanfeng.activity.entity.ActivitySpinwinPersonEntity;
import com.yuanfeng.activity.mapper.ActivitySpinwinPersonMapper;
import com.yuanfeng.activity.service.ActivitySpinwinPersonService;
import com.yuanfeng.activity.vo.PrizeRecordResultVO;
import com.yuanfeng.activity.vo.SpinwinPersonListVO;
import com.yuanfeng.commoms.exception.BizCodeEnume;
import com.yuanfeng.commoms.util.DateUtils;
import com.yuanfeng.commoms.util.ParseMapUtils;
import com.yuanfeng.commoms.util.ParseTokenUtils;
import com.yuanfeng.commoms.util.ResponseResult;
import com.yuanfeng.commoms.util.querypage.PageUtils;
import com.yuanfeng.commoms.util.querypage.Query;
import com.yuanfeng.commoms.vo.user.UserInfoVO;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 */
@Service
public class ActivitySpinwinPersonServiceImpl extends ServiceImpl<ActivitySpinwinPersonMapper, ActivitySpinwinPersonEntity>
    implements ActivitySpinwinPersonService{

    @Override
    public ResponseResult querySpinwinPersonList(Map<String, Object> param) {
        IPage<SpinwinPersonListVO> page=this.baseMapper.querySpinwinPersonList(new Query<SpinwinPersonListVO>().getPage(param),
                param);

        return ResponseResult.success(PageUtils.getPage(page));
    }

    @Override
    public ResponseResult queryPrizeRecord(ActivityExplainParamDTO param) {
        UserInfoVO userInfo = ParseTokenUtils.getUserInfoByToken(param.getToken());
        if (userInfo == null){
            return ResponseResult.fail(BizCodeEnume.TOKEN_IS_ILLEGAL);
        }
        Map<String, Object> map = null;
        try {
            param.setUserId(userInfo.getUserId());
            //将对象转换为map
            Map data = ParseMapUtils.beanToMap(param);
            //分页查询数据
            IPage<PrizeRecordResultVO> page = this.baseMapper.selectPrizeRecord(new Query<PrizeRecordResultVO>().getPage(data), param);
            List<PrizeRecordResultVO> records = page.getRecords();

            if (CollectionUtils.isEmpty(records)) {
                return ResponseResult.fail(0,"暂未有获奖记录");
            }

            // 个人的抽奖记录
            if (param.getIsPersonRecord() == 1) {
                for (PrizeRecordResultVO yfRedpacketTemplate : records) {

                    yfRedpacketTemplate.setOverplusTime(
                            DateUtils.getOverplusTime(new Date(), yfRedpacketTemplate.getUpdateDate()));

                    Integer redpacketState = yfRedpacketTemplate.getRedpacketState();
                    // 如果未领取则可以领取 红包状态(1-未用,2-已用,3-过期
                    // ReceiveOrNot:0未领取1立即使用2不可领取3已用4过期
                    if (null == redpacketState) {
                        yfRedpacketTemplate.setReceiveOrNot(0);
                    } else if (redpacketState == 2) {

                        yfRedpacketTemplate.setReceiveOrNot(3);
                    } else if (redpacketState == 3) {
                        yfRedpacketTemplate.setReceiveOrNot(4);
                    } else {
                        yfRedpacketTemplate.setReceiveOrNot(1);
                    }

                }

            }
            map = new HashMap<>();
            map.put("prizeRecord", records);
            map.put("prizeRecordCount", page.getTotal());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseResult.success(map);
    }
}




